# -*- coding: utf-8 -*-
from Cookie import SimpleCookie
import time
from re import match
import re

#from pycube.core import set_header

class manage_cookie:
    def __init__(self,environ):
        self.environ = environ    
        self.cookie_list = []
  
    def set( self, cookie_key, cookie_val, time="", path="/", domain="", security='' ):    
        iCookie = SimpleCookie( self.environ.get("HTTP_COOKIE", "") )    
        iCookie[cookie_key]=cookie_val
        iCookie[cookie_key]["expires"] = time
        iCookie[cookie_key]["path"] = path
        iCookie[cookie_key]["domain"]= domain    
        
      
        iCookieHeaders = match('^Set-Cookie:(.*?)$', str(iCookie),re.DOTALL)
        if iCookieHeaders:
            iCookieHeaders = iCookieHeaders.group(1) 
            self.cookie_list.append( ('Set-Cookie', iCookieHeaders) )
            return cookie_val
        
    
        
    #===============================================================================
    # #Выгребаем куки
    #===============================================================================
    def get( self, name ):
        c = SimpleCookie( self.environ.get("HTTP_COOKIE", "") )
        if name in c: 
            return c[name].value
        else:
            return False 
        
    def get_cookie_request(self):
        return self.cookie_list

        